package com.wanghua.mall.order.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.Exchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;

/**
 * RabbitMQ实战应用
 *
 * @author wanghua
 * @create 2021-09-10 13:07
 */
@Configuration
public class OrderRabbitMQConfig {

    public static final String ORDER_DELAY_QUEUE_NAME = "order.delay.queue";
    public static final String ORDER_RELEASE_ORDER_QUEUE_NAME = "order.release.order.queue";
    public static final String ORDER_SECKILL_ORDER_QUEUE_NAME = "order.secKill.order.queue";
    public static final String ORDER_EVENT_EXCHANGE_NAME = "order.event.exchange";
    public static final String ORDER_CREATE_ORDER_BINDING_NAME = "order.create.order";
    public static final String ORDER_RELEASE_ORDER_BINDING_NAME = "order.release.order";
    public static final String ORDER_SECKILL_ORDER_BINDING_NAME = "order.secKill.order";

    /**
     * 死信队列
     *
     * @return
     */
    @Bean
    public Queue orderDelayQueue() {
        /**
         * Construct a new queue, given a name, durability flag, and auto-delete flag, and arguments.
         * @param name the name of the queue - must not be null; set to "" to have the broker generate the name.
         * @param durable true if we are declaring a durable queue (the queue will survive a server restart)
         * @param exclusive true if we are declaring an exclusive queue (the queue will only be used by the declarer's
         * connection)
         * @param autoDelete true if the server should delete the queue when it is no longer in use
         * @param arguments the arguments used to declare the queue
         */
        HashMap<String, Object> arguments = new HashMap<>();
        arguments.put("x-dead-letter-exchange", ORDER_EVENT_EXCHANGE_NAME);
        arguments.put("x-dead-letter-routing-key", ORDER_RELEASE_ORDER_BINDING_NAME);
        arguments.put("x-message-ttl", 60000 * 30); // 消息过期时间 1分钟
        return new Queue(ORDER_DELAY_QUEUE_NAME, true, false, false, arguments);
    }

    /**
     * 死信队列转发的队列，被消费者监听
     *
     * @return
     */
    @Bean
    public Queue orderReleaseOrderQueue() {
        return new Queue(ORDER_RELEASE_ORDER_QUEUE_NAME, true, false,
                false, null);
    }

    /**
     * 唯一交换机
     *
     * @return
     */
    @Bean
    public Exchange orderEventExchange() {
        return new TopicExchange(ORDER_EVENT_EXCHANGE_NAME, true,
                false, null);
    }


    @Bean
    public Binding orderCreateOrderBinding() {
        return new Binding(ORDER_DELAY_QUEUE_NAME, Binding.DestinationType.QUEUE,
                ORDER_EVENT_EXCHANGE_NAME, ORDER_CREATE_ORDER_BINDING_NAME, null);
    }

    @Bean
    public Binding orderReleaseOrderBinding() {
        return new Binding(ORDER_RELEASE_ORDER_QUEUE_NAME, Binding.DestinationType.QUEUE,
                ORDER_EVENT_EXCHANGE_NAME, ORDER_RELEASE_ORDER_BINDING_NAME, null);
    }

    /**
     * 订单释放与库存释放绑定
     *
     * @return
     */
    @Bean
    public Binding orderReleaseOtherBinding() {
        return new Binding("stock.release.stock.queue", Binding.DestinationType.QUEUE,
                ORDER_EVENT_EXCHANGE_NAME, "order.release.other.#", null);
    }

    /**
     * 秒杀订单的队列
     */
    @Bean
    public Queue orderSecKillOrderQueue() {
        return new Queue(ORDER_SECKILL_ORDER_QUEUE_NAME, true, false, false, null);
    }

    /**
     * 秒杀订单的队列绑定
     */
    @Bean
    public Binding orderSecKillOrderQueueBinding() {
        return new Binding(ORDER_SECKILL_ORDER_QUEUE_NAME, Binding.DestinationType.QUEUE, ORDER_EVENT_EXCHANGE_NAME,
                ORDER_SECKILL_ORDER_BINDING_NAME, null);
    }


}
